const WebpackBar = require("webpackbar");

/** @type {import("@craco/types").CracoConfig} */
module.exports = {
  babel: {},
  webpack: {
    alias: {},
    plugins: [new WebpackBar()],

    configure: (webpackConfig, { env, paths }) => {
      // paths.appPath='public'
      // paths.appBuild = 'dist'; // 配合输出打包修改文件目录
      // webpackConfig中可以解构出你想要的参数比如mode、devtool、entry等等，更多信息请查看webpackConfig.json文件
      webpackConfig.devtool = "eval-source-map";

      // webpackConfig.module.rules = [
      //   // 部分 node_modules 下 source-map 解析有问题，加此配置不生效
      //   {
      //     test: /\.js$/,
      //     exclude: /node_modules/,
      //     enforce: "pre",
      //     loader: "source-map-loader",
      //   },
      //   ...webpackConfig.module.rules,
      // ];
      // 只是输出规避了错误，有些包的 map 文件加载有问题
      webpackConfig.module.rules.map((v) => {
        if (v.loader?.indexOf("source-map-loader") > -1) {
          v.exclude = /node_modules/;
        }
      });

      /**
       * 修改 output
       */
      webpackConfig.output = {
        ...webpackConfig.output,
        ...{
          // filename: whenDev(() => "static/js/bundle.js", "static/js/[name].js"),
          // chunkFilename: "static/js/[name].[contenthash:8].js",
          // sourceMapFilename: "./datart/[file].map[query]",
        },
        // path: path.resolve(__dirname, 'dist'), // 修改输出文件目录
        publicPath: `${process.env.PUBLIC_URL || ""}/`,
      };
      /**
       * webpack split
       */
      // TODO: 加上后，会卡在 cache 阶段很长时间
      // webpackConfig.optimization.splitChunks =

      var a = {
        ...webpackConfig.optimization.splitChunks,
        ...{
          chunks: "all",
          name: "true",
          cacheGroups: {
            chartGraph: {
              name: "ChartGraph",
              test: /[\\/]app[\\/]components[\\/]ChartGraph[\\/]/,
              priority: 100,
              enforce: true,
            },
            antdDesign: {
              name: "antdDesign",
              test: /[\\/]node_modules[\\/](@ant-design|antd)[\\/]/,
              priority: 100,
              enforce: true,
            },
            videoReact: {
              name: "videoReact",
              test: /[\\/]node_modules[\\/](video-react)[\\/]/,
              priority: 100,
              enforce: true,
            },
            reactGridLayout: {
              name: "reactGridLayout",
              test: /[\\/]node_modules[\\/]react-grid-layout[\\/]/,
              priority: 100,
              enforce: true,
            },
            rc: {
              name: "rc",
              test: /[\\/]node_modules[\\/](rc-tree|react-color|rc-menu|rc-slider|rc-table|rc-util|rc-picker|rc-select|rc-field-form|rc-motion|@babel|@emotion|react-i18next|redux|react-draggable|lodash-es|dnd-core|rc-virtual-list|rc-tabs|rc-trigger|react-dnd|echarts-wordcloud|tinycolor|rc-pagination|rc-drawer|react-quill|rc-input-number|gl-matrix|react-resizable|redux-undo|rc-cascader|rc-image|rc-upload|resize-obserer-polyfill|d3-color|rc-dialog|rc-textarea|rc-overflow)[\\/]/,
              priority: 100,
              enforce: true,
            },
            echarts: {
              name: "echarts",
              test: /[\\/]node_modules[\\/](echarts|zrender)[\\/]/,
              priority: 100,
              enforce: true,
            },
            quill: {
              name: "quill",
              test: /[\\/]node_modules[\\/]quill[\\/]/,
              priority: 100,
              enforce: true,
            },
            lodash: {
              name: "lodash",
              test: /[\\/]node_modules[\\/](lodash|lodash-es)[\\/]/,
              priority: 100,
              enforce: true,
            },
            reactdnd: {
              name: "react",
              test: /[\\/]node_modules[\\/](react-beautiful-dnd|react-dnd)[\\/]/,
              priority: 100,
              enforce: true,
            },
            version: {
              name: "version",
              test: /[\\/]node_modules[\\/](moment|react-dom|core-js|i18next|rc-field-form|buffer|axios|react-redux)[\\/]/,
              priority: 100,
              enforce: true,
            },
          },
        },
      };

      // 返回重写后的新配置
      return webpackConfig;
    },
  },
  devServer: {
    client: {
      overlay: {
        errors: false,
        warnings: false,
      },
      progress: true,
    },
    // before: function (app, server, compiler) {},
    // hot: true,
    // proxy: {},
    // historyApiFallback: {
    //   rewrites: [{ from: /^\/$/, to: "/index.html" }],
    // },
  },
};
